package com.cx.controller;


import com.cx.common.ApiReturn;
import com.cx.pojo.Brand;
import com.cx.pojo.CarInfoModel;
import com.cx.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    //添加品牌
    @RequestMapping("insertBrand")
    @ResponseBody//将数据转换成json后返回给前端
    public ApiReturn insertBrand(Brand brand){
        int rows = brandService.insert(brand);
        if (rows<1){
            return ApiReturn.error("添加品牌失败");
        }
        return ApiReturn.success("添加品牌成功");
    }

    //修改品牌
    @RequestMapping("updateBrand")
    @ResponseBody
    public ApiReturn updateBrand(Brand brand) {
        int rows = brandService.updateByPrimaryKey(brand);
        if (rows < 1) {
            return ApiReturn.error("修改品牌失败");
        }
        return ApiReturn.success("修改品牌成功");
    }

    //删除品牌
    @RequestMapping("deleteBrand")
    @ResponseBody
    public ApiReturn deleteBrand(Integer brandId) {
        int rows = brandService.deleteByPrimaryKey(brandId);
        if (rows < 1) {
            return ApiReturn.error("删除品牌失败");
        }
        return ApiReturn.success("删除品牌成功");
    }

    //根据brandId查询汽车
    @RequestMapping("selectBrandById")
    @ResponseBody
    public ApiReturn selectBrandById(Integer brandId) {
        Brand brandInfo = brandService.selectByPrimaryKey(brandId);
        if (brandInfo != null) {
            return ApiReturn.success("品牌数据",brandInfo);
        }
        return ApiReturn.error("未获取该品牌数据");
    }

    //查询所有汽车品牌
    @RequestMapping("selectAllBrand")
    @ResponseBody
    public ApiReturn selectAllBrand(Brand brand) {
        List<Brand> brands = brandService.selectAll(brand);
        if (brands !=null){
            return ApiReturn.success("返回品牌数据",brands);
        }
        return ApiReturn.error("无数据");
    }

    //根据品牌查询汽车
    @RequestMapping("selectBrandByBrandName")
    @ResponseBody
    public ApiReturn selectBrandByBrandName(String brandName) {
        List<CarInfoModel> carInfoModelList = brandService.selectBrandByBrandName(brandName);
        if (carInfoModelList != null) {
            return ApiReturn.success("返回该品牌所有汽车",carInfoModelList);
        }
        return ApiReturn.error("未获取该品牌汽车数据");
    }

    //根据品牌查询汽车
    @RequestMapping("selectBrand")
    @ResponseBody
    public ApiReturn selectBrand(String brandName) {
        Brand brandInfo = brandService.selectBrand(brandName);
        if (brandInfo != null) {
            return ApiReturn.success("返回该品牌",brandInfo);
        }
        return ApiReturn.error("未获取该品牌数据");
    }
}
